﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Entity.OrganizationManage;

namespace YiSha.Service.YiDongYuan
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-09 22:45
    /// 描 述：尾气检测结果服务类
    /// </summary>
    public class DetectionResultService : RepositoryFactory
    {
        #region 获取数据

        public async Task<List<DetectionResultEntity>> GetList(DetectionResultListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<DetectionResultEntity>> GetPageList(DetectionResultListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<DetectionResultEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<DetectionResultEntity>(id);
        }

        #endregion 获取数据

        #region 提交数据

        public async Task SaveForm(DetectionResultEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
                if (!entity.Data.IsNullOrEmpty())
                {
                    DetectionResultDataEntity dataEntity = new DetectionResultDataEntity()
                    {
                        ResultId = entity.Id,
                        Data = entity.Data
                    };
                    dataEntity.Create();
                    await this.BaseRepository().Insert(dataEntity);
                }
            }
            else
            {
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<DetectionResultEntity>(idArr);
        }

        public async Task<DetectionResultEntity> GetLastResult(long userId, string code)
        {
            var db = this.BaseRepository();
            var userDeviceEntity = await db.FindEntity<DetectionUserDeviceEntity>(t => t.UserId == userId);
            if (userDeviceEntity == null)
            {
                throw new Exception("没有找到当前用户对应的检测设备信息");
            }
            var deviceEntity = await db.FindEntity<DetectionDeviceEntity>(t => t.Id == userDeviceEntity.DeviceId);
            var resultList = await db.FindList<DetectionResultEntity>(t => t.carbrand == code);// && t.DeviceID == deviceEntity.YDJXLH);
            if (resultList.Count() < 1)
            {
                throw new Exception("没有找到检测结果");
            }
            return resultList.OrderByDescending(t => t.testingtime).First();
        }

        #endregion 提交数据

        #region 私有方法

        private Expression<Func<DetectionResultEntity, bool>> ListFilter(DetectionResultListParam param)
        {
            var expression = LinqExtensions.True<DetectionResultEntity>();
            if (param != null)
            {
            }
            return expression;
        }

        #endregion 私有方法
    }
}
